package com.qingcloud.adminbackend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingcloud.adminbackend.entity.equipment.EquipmentDTO;
import com.qingcloud.adminbackend.entity.equipment.OrderEquipmentDTO;
import com.qingcloud.adminbackend.entity.sysorder.*;
import com.qingcloud.adminbackend.entity.sysorder.*;
import com.qingcloud.adminbackend.entity.sysorg.SysBusinessTypeDTO;
import com.qingcloud.adminbackend.entity.sysorg.SysOrgOrderDTO;
import com.qingcloud.adminbackend.entity.sysorg.SysUserModuleDTO;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 订单表 Mapper 接口
 * </p>
 *
 * @author liyuhong
 * @since 2018-12-20
 */
public interface SysOrderMapper extends BaseMapper<SysOrder> {

    /**
     * 查询订单列表
     *
     * @param page
     * @param userId
     * @param sysOrder
     * @return
     */
    IPage<SysOrder> findSysOrderList(Page<SysOrderPageDTO> page, @Param("userId") String userId,
                                     @Param("sysOrder") SysOrderPageDTO sysOrder, @Param("orderIdSet") Collection<String> orderIdSet);

    /**
     * 添加订单
     *
     * @param sysOrder
     */
    void addSysOrder(@Param("sysOrder") SysOrder sysOrder);

    /**
     * 添加订单
     *
     * @param sysContract
     */
    void addSysContract(@Param("sysContract") SysOrder sysContract);

    /**
     * 编辑订单
     *
     * @param sysOrder
     */
    void updateSysOrder(@Param("sysOrder") SysOrder sysOrder);

    /**
     * 编辑合同
     *
     * @param sysContract
     */
    void updateSysContract(@Param("sysContract") SysOrder sysContract);

    /**
     * 订单管理客户
     *
     * @param orderId
     * @param orgId
     */
    void toOrderOrg(@Param("orderId") String orderId, @Param("orgId") String orgId);

    /**
     * 删除订单
     *
     * @param orderIds
     */
    void deleteOrder(@Param("orderIds") List<String> orderIds);

    /**
     * 删除订单关联功能
     *
     * @param orderIds
     */
    void deleteOrderFunction(@Param("orderIds") List<String> orderIds);

    /**
     * 根据订单查询订单关联的设备
     *
     * @param page
     * @param equipmentDTO
     * @return
     */
    IPage<OrderEquipmentDTO> findOrderEquipmentList(Page<OrderEquipmentDTO> page, @Param("equipmentDTO") EquipmentDTO equipmentDTO, @Param("businessType") Integer
            businessType);

    /**
     * 查询订单详情
     *
     * @param orderId
     * @return
     */
    SysOrder findOrderById(@Param("orderId") String orderId);

    /**
     * 订单绑定功能
     *
     * @param functionIds
     * @param sysOrder
     */
    void insertOrderFunction(@Param("functionIds") Set<String> functionIds, @Param("sysOrder") SysOrder sysOrder);

    /**
     * 客户关联的订单
     *
     * @param page
     * @param sysOrgOrderDTO
     * @return
     */
    IPage<SysOrder> findOrgOrderByOrgId(Page<SysOrgOrderDTO> page, @Param("sysOrgOrderDTO") SysOrgOrderDTO sysOrgOrderDTO);

    /**
     * 解除订单与客户的关系
     *
     * @param ids
     */
    void updateSysOrderByOrgIds(@Param("ids") List<String> ids);

    /**
     * 解除订单中的业务种类与客户的关系
     * @param ids
     */
    void updateBusinessTypeByOrgIds(@Param("ids") List<String> ids);

    /**
     * 没有关联客户的订单Api
     *
     * @param businessTypeSet
     * @return
     */
    List<Map> findOrderByBusinessType(@Param("businessTypeSet") Collection<Integer> businessTypeSet);

    /**
     * 已关联的订单
     *
     * @param orgId
     * @return
     */
    List<Map> findOrderByOrgId(@Param("orgId") String orgId);

    /**
     * 客户批量关联订单
     *
     * @param sysOrderBO
     */
    void orgToOrders(@Param("sysOrderBO") SysOrderBO sysOrderBO);

    /**
     * 用户授权【运维】读写权限的订单
     *
     * @return
     */
    List<SysUserOrderMgrDTO> getOrders(@Param("sysUserOrderMgrDTO") SysUserOrderMgrDTO sysUserOrderMgrDTO);

//    /**
//     * 用户所属【销售】的订单
//     *
//     * @return
//     */
//    List<SysUserOrderMgrDTO> subordinateOrders(@Param("sysUserOrderMgrDTO") SysUserOrderMgrDTO sysUserOrderMgrDTO);

    /**
     * 用户已授权【运维】的订单
     *
     * @return
     */
    List<SysUserOrderMgrDTO> grantedOrders(@Param("sysUserOrderMgrDTO") SysUserOrderMgrDTO sysUserOrderMgrDTO);

//    /**
//     * 用户已选【数据质控】的订单
//     *
//     * @param sysUserOrderMgrDTO
//     * @return
//     */
//    List<SysUserOrderMgrDTO> dataQuality(@Param("sysUserOrderMgrDTO") SysUserOrderMgrDTO sysUserOrderMgrDTO);

    /**
     * 客户及分支机构对应的订单
     *
     * @param orgId
     * @return
     */
    List<Map> findOrderListByOrgId(@Param("orgId") String orgId);

    /**
     * 验证订单下是否存在设备
     *
     * @param orderIds
     * @return
     */
    Long findOrderEquipments(@Param("orderIds") List<String> orderIds);

    /**
     * 用户授权【运维】读写权限的订单完全管理权限
     *
     * @return
     */
    Set<SysUserOrderMgrDTO> getAllOrders(@Param("sysUserOrderMgrDTO") SysUserOrderMgrDTO sysUserOrderMgrDTO);

    /**
     * 验证客户是否关联订单
     *
     * @param ids
     * @return
     */
    long findOrderByOrgIds(@Param("ids") List<String> ids);

    /**
     * 用户授权【售前】订单
     *
     * @param sysUserOrderMgrDTO
     * @return
     */
    List<SysUserOrderMgrDTO> getRetrieveOrders(@Param("sysUserOrderMgrDTO") SysUserOrderMgrDTO sysUserOrderMgrDTO);

    /**
     * 客户相关联的订单
     *
     * @param orgIds
     * @return
     */
    List<SysUserModuleDTO> getOrderByOrgIds(@Param("orgIds") List<String> orgIds, @Param("parameter") String parameter, @Param("businessType") String businessType);

    /**
     * 用户拥有客户写权限下的订单
     *
     * @param userId
     * @param isAdmin
     * @return
     */
    List<SysUserModuleDTO> findOrgMgrOrderByUserId(@Param("userId") String userId, @Param("isAdmin") boolean isAdmin, @Param("businessType") String businessType);

    /**
     * 查询用户读取权限的订单
     */
    Set<String> findUserReadOrderId(@Param("userId") String userId, @Param("isAdmin") boolean isAdmin);

    /**
     * 验证订单是否存在
     *
     * @param orderId
     * @return
     */
    String isDeleteOrderById(@Param("orderId") String orderId);

    /**
     * 批量验证订单是否存在
     *
     * @param orderIds
     * @return
     */
    List<String> isDeleteOrderByIds(@Param("orderIds") List<String> orderIds);

    List<String> getAllManageOrderId(@Param("userId") String userId);

//    List<SysBusinessTypeDTO> getBusinessType(@Param("orgId") String orgId, @Param("isAdmin") boolean isAdmin);

    List<SysBusinessTypeDTO> getBusinessTypeByUserId(@Param("userId") String userId, @Param("isAdmin") boolean isAdmin,@Param("orgAdmin") boolean orgAdmin);

    List<SysBusinessTypeDTO> getRoleByBusinessType(@Param("businessType") String businessType);

    /**
     * 用户业务种类下有权限订单
     *
     * @return
     */
    Set<SysUserOrderMgrDTO> getAllOrdersByBusinessType(@Param("orderRequest") SysUserOrderRequest orderRequest);


    SysBusinessApp  findByBizCode(@Param("origin") int origin ,@Param("bizCode") String bizCode);

    SysBusinessApp  findByCode(@Param("bizCode") String bizCode);
}
